const productDao = require('../dao/productDao');

module.exports = {
    /**
     * 根据商品分类名称获取首页商品信息
     * @param {*} ctx 
     */
    getHomeProduct: async ctx => {
        const { categoryName } = ctx.request.query;
        // 根据分类的名称查询分类表获取对于的商品id
        const category_id = await productDao.getCategoryIdByName(categoryName);
        // 根据分类id获取首页分类产品信息
        const productList = await productDao.getHomeProduct(category_id);
        ctx.body = {
            status: 80200,
            message: 'success',
            data: productList
        }
    },
    /**
     * 根据商品分类名称获取首页热门商品的信息
     * @param {*} ctx 
     */
    getHotProduct: async ctx => {
        const { categoryName } = ctx.request.body;
        const categoryID = [];
        // 根据分类的名称查询分类表获取对于的商品id
        for (let i = 0; i < categoryName.length; i++) {
            const category_id = await productDao.getCategoryIdByName(categoryName[i]);
            categoryID.push(category_id)
        }
        // 根据分类id获取首页分类产品信息
        const productList = await productDao.getHotProduct(categoryID, 0, 7);
        ctx.body = {
            status: 80200,
            message: 'success',
            data: productList
        }
    },
    /**
     * 获取分类
     * @param {*} ctx 
     */
    getCategory: async ctx => {
        const categoryList = await productDao.getCategory()
        ctx.body = {
            status: 80200,
            message: 'success',
            data: categoryList
        }
    },
    /**
     * 获取全部商品
     * @param {*} ctx 
     */
    getAllproduct: async ctx => {
        const { currentPage, pageSize } = ctx.request.body;
        // 计算页尺寸
        const offset = (currentPage - 1) * pageSize
        const productList = await productDao.getAllproduct(offset, pageSize);
        const total = await productDao.getAlltotal()
        ctx.body = {
            status: 80200,
            message: 'success',
            data: { data: productList, total: total[0]['count(1)'] }
        }
    },
    /**
     * 根据分类获取商品
     * @param {*} ctx 
     */
    getProductByCategory: async ctx => {
        const { categoryID, currentPage, pageSize } = ctx.request.body;
        // 计算分页
        const offset = (currentPage - 1) * pageSize;
        const [productList, total] = await productDao.getProductByCategory(categoryID, offset, pageSize);
        // const total = await productDao.getTotalByCategory(categoryID);
        console.log(productList, total)
        ctx.body = {
            status: 80200,
            message: 'success',
            data: { data: productList, total: total[0]['count(1)'] }
        }
    },
    /**
     * 根据商品id获取商品详情
     * @param {*} productID 
     * @returns 
     */
    getProductDetailsById: async ctx => {
        const { productID } = ctx.request.query;
        const detailsList = await productDao.getProductDetailsById(productID);
        ctx.body = {
            status: 80200,
            message: 'success',
            data: detailsList
        }
    },
      /**
     * 根据商品id获取商品图片
     * @param {*} productID 
     * @returns 
     */
    getProductPictureById: async ctx => {
        const { productID } = ctx.request.query;
        const pictureList = await productDao.getProductPictureById(productID);
        ctx.body = {
            status: 80200,
            message: 'success',
            data: pictureList
        }
    }
}